<?php
	include_once 'isLogin.php';

	$table_name = "companies";
	$table_detail = "companies_detail";
	$key_index = array('title','content','upid','state');
	$where = ' where ';
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'禁用','1'=>'可用'),
			);
	
	//添加
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
		
		if($id!=''){
			$return='<tr line_id="'.$id.'">
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td value_id="'.$id.'">'.$data['title'].'</td>
			<td value_id="'.$id.'">'.$data['content'].'</td>
			<td>&nbsp;</td>
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
			$return.='</td></tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}
		echo json_encode($arr);
		exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		$delFile = array(
				array('key'=>'img','dir'=>'../upload/test/large/'),
				array('key'=>'img','dir'=>'../upload/test/middle/'),
				array('key'=>'img','dir'=>'../upload/test/small/'),
				array('key'=>'file','dir'=>'../upload/test/file/'),
		);
		$relation=array(
				array('table'=>$table_detail,'key'=>'company_id'),
				
		);
		echo $commonc->del($table_name,$_POST['id'],$delFile,$relation);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改前台显示的公司
	if($_GET['action']=='state'){
		if($_POST['key']=='是'){
			$arr=array('is_top'=>'0');
		}else{
			$arr=array('is_top'=>'1');
		}
		echo $commonc->update($table_name,$arr,"id='".$_POST['id']."'");
		exit();
	}
	//修改前台显示的广告位
	if($_GET['action']=='states'){
		if($_POST['key']=='展示中'){
			$arr=array('is_img'=>'0');
		}else{
			$arr=array('is_img'=>'1');
		}
		echo $commonc->update($table_name,$arr,"id='".$_POST['id']."'");
		exit();
	}
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}
	//搜索
	$where ='';
	if($_POST['submit']!= ''){
		
		if($_POST['company_name']!=''&&$_POST['company_name']!=null)
		{
			$where = " where c.title like '%".$_POST['company_name']."%'";
		}
		
		if($_POST['star']!=''&&$_POST['star']!=null)
		{
			if($where =='')
			{
				$where = " where c.star = '".$_POST['star']."'";
			}
			else
			{
				$where .= " and c.trade_id = '".$_POST['company_trade']."'";
			}
			
		}
		if($_POST['company_level']!=''&&$_POST['company_level']!=null)
		{
			if($where =='')
			{
				$where = " where c.level_id = '".$_POST['company_level']."'";
			}
			else
			{
				$where .= " and c.level_id = '".$_POST['company_level']."'";
			}
			
		}
	}
	$where .= $where != '' ? ' and ' : ' WHERE ';
	$smarty->assign("upid",$upid);
	$smarty->assign("search",$_POST);
	$sql = $table_name.' c left join companies_level cl on c.level_id=cl.ID ';
	$sql.= $where.' c.state = 2 ';
	$sql.= " order by c.id desc ";
	$row=$commonc->getList($sql,$sql);
	
	if(is_array($row['result'])){
		//得到状态
		foreach ($row['result'] as $k => $v) {
                    if($v['state']=='1' || $v['is_img']=='1')
                    {
                        $v['state'] = '审核中';
						$v['is_img']='展示中';
                    }
					if($v['is_top']=='1'){
						$v['is_top']="是";
					}
					if($v['is_top']=='0'){
						$v['is_top']="否";
					}
                     if($v['state']=='2'|| $v['img']=='0' )
                    {
                         $v['is_img'] = '无';
                         $v['state'] = '通过审核';
                        
                    }
                   $row['result'][$k] = $v;
                    
                }
	}
	
       
	$smarty->assign("all_level",$all_level['result']);
	$smarty->assign("all_trade",$all_trade['result']);
	$smarty->assign("row",$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
?>